<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sfWidgetFormDoctrineChoiceImages
 *
 * @author tempo123web
 */
class sfWidgetFormInputSelectWithImg extends sfWidgetForm {
    public function render($name, $value = null, $attributes = array(), $errors = array()){
           $choices = $this->getOption('choices');
           if ($choices instanceof sfCallable)
           {
             $choices = $choices->call();
           }
           $str='';          
//var_dump($name,$value ,$attributes,$errors,$choices); die();
        if(empty ($attributes["multiple"])){
            //throw new Exception("solo soporta multiple");
            
            foreach($choices  as $key=>$val){
                $params=json_decode($val,true);
                if(!is_array($params)){
                    throw new Exception("el texto debe ser un arreglo en json");
                }
                $id=$this->generateId($name, $key);
                $id.="_".$key;
                $str.=strtr("
                            <div class='select_option'>
                                <input class='chk_img' type='radio' name='%name%' %checked% value='%value%' id='%id%' />
                                <label for='%id%'>
                                    <img title='%caption%' height='50px' width='80px' src='%src%' alt='%caption%' /><br/>
                                    <span class='caption'></span>
                                </label>
                            </div>
                            ",
                        array("%name%"=>$name,
                             "%id%"=>$id,
                             "%src%"=>$params['src'],
                             "%value%"=> $key,
                            "%caption%"=>$params['caption'],
                            "%checked%"=> ($key == $value)?"checked":""));

            }
        }  else {
        foreach($choices  as $key=>$val){
            $params=json_decode($val,true);
            if(!is_array($params)){
                throw new Exception("el texto debe ser un arreglo en json");
            }
            $id=$this->generateId($name, $key);            
            $str.=strtr("
                        <div class='select_option'>
                            <input class='chk_img' type='checkbox' name='%name%' %checked% value='%value%' id='%id%' />
                            <label for='%id%'>
                                <img title='%caption%' height='50px' width='80px' src='%src%' alt='%caption%' /><br/>
                                <span class='caption'></span>
                            </label>
                        </div>
                        ",
                    array("%name%"=>$name,
                         "%id%"=>$id,
                         "%src%"=>$params['src'],
                         "%value%"=> $key,
                        "%caption%"=>$params['caption'],
                        "%checked%"=> in_array($key, $value)?"checked":""));
            
        }
        }
        
        

        
        
        $str =" <div class='select_content'>"
                   .$str.
              " </div>
                <style type='text/css'>
                    .chk_img{
                        top: -2px;
                        left: -3px;
                        position: absolute;
                    }
                    .select_option{
                        float:left;
                        position: relative;
                    }
                    .caption{
                        width: 80px;                                
                        display: block;
                        overflow: hidden;
                    }
                    .select_content{
                        width: 337px;
                        height: 150px;
                        overflow: auto;
                    }

                </style>";
        
        return $str;
                    
    }
    
    protected function configure($options = array(), $attributes = array()) {
        $this->addRequiredOption("choices");
        parent::configure($options, $attributes);
    }
}

?>
